#!D:\Python312
# 获取联系人

from pywinauto import mouse
from pywinauto.application import Application
# 打开任意一个安装程序
app = Application(backend="uia").connect(title="微信")
# app = Application(backend="uia").connect(process=15148)
print(app)

dlg = app["微信"]

# 点击通讯录
dlg.child_window(title="通讯录", control_type="Button").click_input()
# 定义存储联系人列表
nameList = []

# 获取列表
elementList = dlg.child_window(title="联系人", control_type="List")


# 处理获取每一项联系人的高度
linElement = elementList.items()
listIndex =  list(reversed(linElement))[1]
linHeight = listIndex.rectangle().height()
scrollHeight = linHeight * (len(linElement)-2)/120

def toGetname(elementList):
    infoNameList = elementList.items()
    for index, itm in enumerate(infoNameList):
        if itm.element_info.name!="":
            if index==len(infoNameList)-1 and itm.element_info.name in nameList:
                print("触底了退出")
                return None,"触底了退出"
            print(itm.element_info.name)
            nameList.append(itm.element_info.name)
    return None,None

# 获取联系人名称
# 初次进来获取联系人
toGetname(elementList)

# 列表滚动再次获取联系人
while True:
    # 滚动列表
    liaotian = elementList.rectangle()
    # 滚动聊天窗口
    mouse.scroll(coords=(liaotian.left + 20, liaotian.top + 20), wheel_dist=-int(scrollHeight))
    _,err = toGetname(elementList)
    if err!=None:
        break
